<?php

namespace app\traits;

use app\admin\model\Product as ProductModel;

trait Home
{
   protected $model,$pages;

    public function index(){
        $cid=$this->request->param('cid');
        $where=[];
        $query=[];
        if ($cid) {
            $where["category_id"]=$cid;
            $query['cid']=$cid;

        }

        $data=$this->model->where($where)->order('id','desc')->paginate([
            'query' => $query,
            'list_rows' =>$this->pages,
        ]);
        $this->assign([
            'data'=>$data,
            'cid'=>$cid,

        ]);
        return $this->fetch();
    }
    public function detail(){
        $id=$this->request->param('id');
        $detail=$this->model->find($id);
//        halt($detail);
        $prev=$this->model->where('id','>',$id)->order('id','asc')->find();
        $next=$this->model->where('id','<',$id)->order('id','desc')->find();
//        更新浏览次数
//        $detail['views']=$detail['views']+1;
        $detail->save(['views'=>$detail['views']+1]);
        $this->assign([
            'detail'=>$detail,
            'prev'=>$prev,
            'next'=>$next,
            'cid'=>$detail['category_id'],

        ]);
        return $this->fetch();
    }
}